package com.shzu.jwc.ty.service;

import java.util.List;

import com.shzu.jwc.ty.bean.PeTheoreticalTeacher;
import com.shzu.jwc.ty.bean.TheoreticalExamination;
import com.shzu.jwc.ty.bean.TheoreticalExaminationAppointment;
import com.shzu.jwc.ty.bean.TheoreticalExaminationExample;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.ReadImportStu;
import com.shzu.jwc.ty.dao.PeTheoreticalTeacherMapper;
import com.shzu.jwc.ty.dao.TheoreticalExaminationAppointmentMapper;
import com.shzu.jwc.ty.dao.TheoreticalExaminationMapper;
import com.shzu.jwc.ty.param.StartandEndDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shzu.jwc.ty.common.Semester;
import org.springframework.web.multipart.MultipartFile;

@Service
public class TheoreticalExaminationservice {

	@Autowired
	TheoreticalExaminationMapper mapper;

	@Autowired
	PeTheoreticalTeacherMapper peTheoreticalTeacherMapper;
	
	@Autowired
	SystemYearservice systemYearservice;

	@Autowired
	StudentInfoservice studentInfoservice;

	@Autowired
	SystemConfigservice systemConfigservice;

	@Autowired
	Studytimeservice studytimeservice;

	@Autowired
	TheoreticalExaminationAppointmentMapper tmapper;
	
	public List<TheoreticalExamination> getkowlegetest()
	{
		Integer systemYearId=systemYearservice.getoneyearby(Semester.getnow());
		TheoreticalExaminationExample examinationExample=new TheoreticalExaminationExample();
		TheoreticalExaminationExample.Criteria criteria=examinationExample.createCriteria();
		criteria.andSystemYearIdEqualTo(systemYearId);
		criteria.andTheoreticalExaminationStateEqualTo(0);
		return mapper.selectByExample(examinationExample);
	}

	public List<TheoreticalExamination> getknowlege(Page page){
		return mapper.getknowlege(null,0,systemYearservice.getoneyearby(Semester.getnow()),page.getStartPos(),page.getPageSize());
	}

	public List<TheoreticalExamination> byweeksearch(Integer systemExamWeeklyId,Page page){
		return mapper.getknowlege(systemExamWeeklyId,0,systemYearservice.getoneyearby(Semester.getnow()),page.getStartPos(),page.getPageSize());
	}

	public Integer countknowlege(){
		return (int)mapper.countknowlege(null,0,systemYearservice.getoneyearby(Semester.getnow()));
	}

	public Integer countselectknowlege(Integer systemYearId){
		return (int)mapper.countknowlege(null,0,systemYearId);
	}

	public Integer countselectweek(Integer systemExamWeeklyId){
		return (int)mapper.countknowlege(systemExamWeeklyId,0,systemYearservice.getoneyearby(Semester.getnow()));
	}

	public Integer inserttheoretical(TheoreticalExamination theoreticalExamination)
	{
		Integer systemYearId=systemYearservice.getoneyearby(Semester.getnow());
		theoreticalExamination.setSystemYearId(systemYearId);
		theoreticalExamination.setTheoreticalExaminationState(0);
		return mapper.insertSelective(theoreticalExamination);
	}
	
	public Integer delete(Integer theoreticalExaminationId)
	{
		TheoreticalExamination theoreticalExamination=new TheoreticalExamination();
		theoreticalExamination.setTheoreticalExaminationState(3);
		theoreticalExamination.setTheoreticalExaminationId(theoreticalExaminationId);
		return mapper.updateByPrimaryKeySelective(theoreticalExamination);
	}
	
	public TheoreticalExamination getone(Integer theoreticalExaminationId)
	{
		return mapper.selectByPrimaryKey(theoreticalExaminationId);
	}
	
	public Integer update(TheoreticalExamination theoreticalExamination)
	{
		return mapper.updateByPrimaryKeySelective(theoreticalExamination);
	}
	
	public List<TheoreticalExamination> search(String theoreticalExaminationName)
	{
		TheoreticalExaminationExample examinationExample= new TheoreticalExaminationExample();
		TheoreticalExaminationExample.Criteria criteria=examinationExample.createCriteria();
		criteria.andTheoreticalExaminationStateEqualTo(0);
		criteria.andTheoreticalExaminationNameLike("%"+theoreticalExaminationName+"%");
		return mapper.selectByExample(examinationExample);
	}
	
	public List<TheoreticalExamination> bydatesearch(Integer systemYearId,Page page)
	{
		return mapper.getknowlege(null,0,systemYearId,page.getStartPos(),page.getPageSize());
	}
	
	public List<String> bydategetname(){
		return mapper.bydategetname(systemYearservice.getoneyearby(Semester.getnow()));
	}

	public Integer copyone(Integer theoreticalExaminationId){
		TheoreticalExamination theoreticalExamination=mapper.selectByPrimaryKey(theoreticalExaminationId);
		theoreticalExamination.setTheoreticalExaminationId(null);
		theoreticalExamination.setTheoreticalExaminationAddTime(null);
		return mapper.insertSelective(theoreticalExamination);
	}

	public List<PeTheoreticalTeacher> showprint(){
		return peTheoreticalTeacherMapper.selectByExample(null);
	}

	public List<PeTheoreticalTeacher> selectshowpint(StartandEndDate startandEndDate){
		return peTheoreticalTeacherMapper.selectshowprint(startandEndDate);
	}

	public void importStu(String name, MultipartFile file,Integer theoreticalExaminationId){
		ReadImportStu readImportStu=new ReadImportStu();
		List<String> stuNums=readImportStu.getExcelInfo(name,file);
		Integer studytime=Integer.parseInt(systemConfigservice.getvalue("studytime"));

		for(int i=0;i<stuNums.size();i++){
			if(studytimeservice.byStuNumGetStuTime(stuNums.get(i))==null||studytimeservice.byStuNumGetStuTime(stuNums.get(i))>=studytime) {
				Integer userInfoId = studentInfoservice.searchstudent(stuNums.get(i)).get(0).getUserStudentInfoId();
				TheoreticalExaminationAppointment appointment=new TheoreticalExaminationAppointment();
				appointment.setTheoreticalExaminationId(theoreticalExaminationId);
				appointment.setUserInfoId(userInfoId);
				appointment.setTheoreticalExaminationAppointmentState(1);
				appointment.setTheoreticalExaminationAppointmentSign("正常");
				appointment.setTheoreticalExaminationSign(1);
				tmapper.insert(appointment);
			}
			else{
				System.out.println("时长没刷够的学生："+stuNums.get(i));
			}
		}
	}
}
